﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prueba.ViewModel.Utils
{
    public class Threadable : ViewModelBase
    {
        public Func<int> Function
        {
            get { return _Function; }
            set
            {
                if (_Function != value)
                {
                    _Function = value;
                    OnPropertyChanged(FunctionPropertyName);
                }
            }
        }
        private Func<int> _Function;
        public const string FunctionPropertyName = "Function";
 
        public System.Timers.Timer Timer
        {
            get { return _Timer; }
            set
            {
                if (_Timer != value)
                {
                    _Timer = value;
                    OnPropertyChanged(TimerPropertyName);
                }
            }
        }
        private System.Timers.Timer _Timer;
        public const string TimerPropertyName = "Timer";

        public void Tick(Object sender, System.Timers.ElapsedEventArgs args)
        {
            this.Timer.Enabled = false;
            ((System.Timers.Timer)sender).Stop();

            this.Function();
        }

        public Threadable(Func<int> Function)
        {
            this.Function = Function;

            this.Timer = new System.Timers.Timer(100);
            this.Timer.Enabled = true;
            this.Timer.Elapsed += new System.Timers.ElapsedEventHandler(this.Tick); 
        }

        public void Start()
        {
            this.Timer.Start();
        }
    }
}
